//
//  MYXSettingCell.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/28.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYXSettingCell.h"
#import "MYY_Me_Macro.h"
#import "NSString+Additions.h"

#define kBasicCellHeight 44
typedef void(^SwitchChangeBlock)(BOOL onState);
@interface MYXSettingCell()

@property(nonatomic, strong) UIView *topView;
@property(nonatomic, strong) UILabel *nameLabel; //名称
@property(nonatomic, strong) UIImageView *indicatorView; //>箭头
@property(nonatomic, strong) UILabel *detailLabel; //说明文字，如：关闭之后将不再显示..
@property(nonatomic, strong) UILabel *infoLabel; //如:缓存的0.3M
@property(nonatomic, strong) UISwitch *switchButton; //切换按钮
@property(nonatomic, strong) UIView *seperatorView;

@property(nonatomic, assign) CGFloat topHeight;

@property(nonatomic, copy) SwitchChangeBlock switchChangeBlock;

@end


@implementation MYXSettingCell


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"MYXSettingCell";
    MYXSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[MYXSettingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.selectionStyle          = UITableViewCellSelectionStyleNone;
        cell.backgroundColor         = [UIColor whiteColor];
    }
    
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initCreatUI];
        
    }
    return self;
}

-(void)initCreatUI
{
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.indicatorView];
    [self.contentView addSubview:self.detailLabel];
    [self.contentView addSubview:self.infoLabel];
    [self.contentView addSubview:self.switchButton];
    [self.contentView addSubview:self.seperatorView];
    
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.topView.frame = CGRectMake(0, 0, WIDTH, self.topHeight);
    self.nameLabel.frame = CGRectMake(15, self.topHeight, WIDTH, kBasicCellHeight);
    self.indicatorView.frame = CGRectMake(WIDTH-20-8, self.topHeight, 8, kBasicCellHeight);
    self.detailLabel.frame = CGRectMake(15, kBasicCellHeight+self.topHeight, WIDTH - 40, 18);
    [self.detailLabel sizeToFit];
    self.infoLabel.frame = CGRectMake(WIDTH/2.0, self.topHeight, WIDTH/2.0-20-14, kBasicCellHeight);
    self.switchButton.frame = CGRectMake(WIDTH-20-50, self.topHeight+5, 20, kBasicCellHeight-5);
    self.seperatorView.frame = CGRectMake(0, self.bounds.size.height-0.5, WIDTH, 0.5);
}

- (void)setSettingName:(NSString *)settingName detailText:(NSString *)detailText infoText:(NSString *)infoText indicator:(BOOL)indicator switchButton:(BOOL)switchButton topHeight:(CGFloat)topHeight switchChanged:(void (^)(BOOL on))switchChanged
{
    self.switchChangeBlock = switchChanged;
    self.topHeight = topHeight;
    self.nameLabel.text = settingName;
    self.detailLabel.text = detailText;
    self.infoLabel.text = infoText;
    self.indicatorView.hidden = !indicator;
    self.switchButton.hidden = !switchButton;
    [self setNeedsLayout];
}

- (void)changeSwitchState:(BOOL)selected
{
    [self.switchButton setOn:selected];
}

+ (CGFloat)cellHeightWithDetailText:(NSString *)detailText topHeight:(CGFloat)topHeight
{
    if ([detailText isEqualToString:@""]) {
        return kBasicCellHeight + topHeight;
    }
    CGSize detailHeight = [detailText sizeWithConstrainedToWidth:WIDTH-40 fromFont:[UIFont systemFontOfSize:12] lineSpace:0 numberOfLines:0];
    return kBasicCellHeight + detailHeight.height + 8 + topHeight;
}

-(void)switchAction:(id)sender
{
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    if (self.switchChangeBlock) {
        self.switchChangeBlock(isButtonOn);
    }
}

#pragma mark - property
- (UIView *)topView
{
    if (_topView == nil) {
        _topView = [UIView new];
        _topView.backgroundColor = RGBACOLOR(238, 238, 238, 1.0);
    }
    return _topView;
}

- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = [UILabel new];
        _nameLabel.numberOfLines = 1;
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = [UIColor blackColor];
    }
    return _nameLabel;
}

- (UIImageView *)indicatorView
{
    if (_indicatorView == nil) {
        _indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"youxiangjian"]];
        _indicatorView.contentMode = UIViewContentModeCenter;
    }
    return _indicatorView;
}

- (UILabel *)detailLabel
{
    if (_detailLabel == nil) {
        _detailLabel = [UILabel new];
        _detailLabel.numberOfLines = 0;
        _detailLabel.font = [UIFont systemFontOfSize:12];
        _detailLabel.textColor =MYYColorFromRGB(0x939ba6);
    }
    return _detailLabel;
}

- (UILabel *)infoLabel
{
    if (_infoLabel == nil) {
        _infoLabel = [UILabel new];
        _infoLabel.numberOfLines = 1;
        _infoLabel.font = [UIFont systemFontOfSize:16];
        _infoLabel.textColor = MYYColorFromRGB(0x939ba6);
        _infoLabel.textAlignment = NSTextAlignmentRight;
    }
    return _infoLabel;
}

- (UISwitch *)switchButton
{
    if (_switchButton == nil) {
        _switchButton = [UISwitch new];
        _switchButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [_switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchButton;
}

- (UIView *)seperatorView
{
    if (_seperatorView == nil) {
        _seperatorView = [UIView new];
        _seperatorView.backgroundColor = RGBACOLOR(238, 238, 238, 1.0);
    }
    return _seperatorView;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
